package com.wangjie.secondary;

/**
 * @author jieshao
 * @date 2022/5/27 9:45
 *
 * 7. 整数反转
 */
public class Test02 {
    public static void main(String[] args) {
        int a = 123;
        int b = -123;
        int res1 = reverse(a);
        int res2 = reverse(b);
        System.out.println("a = " + res1);
        System.out.println("b = " + res2);
    }

    public static int reverse(int x) {
        int res = 0;
        while (x != 0) {
            if (res < Integer.MIN_VALUE / 10 || res > Integer.MAX_VALUE / 10) {
                return 0;
            }
            res = res * 10 + x % 10;
            x /= 10;
        }
        return res;
    }
}